/**
 * 
 */
package net.homelinux.ajay.net.ondisk.view.client;

import net.homelinux.ajay.net.ondisk.client.PropertyPanel;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author ajay
 *
 */
public abstract class AppTreeItem extends TreeItem {

//  private VerticalPanel vertical;

  //  private void init() {
  //    Widget widget = this.getWidget();
  //    vertical = new VerticalPanel();
  //    if(widget!=null){
  //      vertical.add(widget);
  //    }else{
  //      vertical.add(new Label  (this.getText()));
  //    }
  //    this.setWidget(vertical);
  //  }
  
  
  //  public void removeContent(Panel dragTarget) {
  //    vertical.remove(dragTarget);
  //  }
  //  public void addContent(Panel dragTarget) {
  //    vertical.add(dragTarget);
  //  }
    
    private DockPanel content;
    private CommandsMenu contextmenu;

  /**
   * 
   */
  public AppTreeItem() {
    super();
    init(null,null);
  }

  /**
   * @param html
   */
  public AppTreeItem(String html, Image img) {
    super();
    init(html,img);
  }

  private void init(String html, Image img) {
    //this.sinkEvents(Event.ONCLICK);
    content = new DockPanel();
    setWidget(content);
    if(img!=null){
      content.add(img,DockPanel.WEST);
    }
    
    if(html!=null){
      Label label = new Label(html);
      label.setWordWrap(false);
      content.add(label,DockPanel.CENTER);
    }
    
//    
//    CommandsMenu commandsMenu = getCommandsMenu();
//    if(commandsMenu!=null){
//      content.add(commandsMenu);
//    }
    
  }
  
  

//  /**
//   * @param widget
//   */
//  public AppTreeItem(Widget widget) {
//    super(widget);
////    init();
//  }


  public void onDeselect() {
    content.remove(contextmenu);
  }

  public void onSelect() {
    contextmenu = getCommandsMenu();
    content.add(contextmenu , DockPanel.SOUTH);
    
    PropertyPanel props = getAppTree().getAppPanel().getPropertyPanel();
    props.refresh(this);
  }
  
  private AppTree getAppTree() {
    return (AppTree) getTree();
  }

//  private void init() {
//    Widget widget = this.getWidget();
//    vertical = new VerticalPanel();
//    if(widget!=null){
//      vertical.add(widget);
//    }else{
//      vertical.add(new Label  (this.getText()));
//    }
//    this.setWidget(vertical);
//  }


//  public void removeContent(Panel dragTarget) {
//    vertical.remove(dragTarget);
//  }
//  public void addContent(Panel dragTarget) {
//    vertical.add(dragTarget);
//  }
  
  public void onContextClick(Event event) {
//    CommandsMenu menu = getCommandsMenu();
//    if(menu!=null){
////      menu.setPopupPosition(DOM.eventGetClientX(event)-getAbsoluteLeft(), DOM.eventGetClientY(event)-getAbsoluteTop());
//      menu.setPopupPosition(DOM.eventGetClientX(event), DOM.eventGetClientY(event));
//      menu.show();
//    }
  }

  protected abstract CommandsMenu getCommandsMenu();

  public Widget getMenuBar() {
    CommandsMenu commandsMenu = getCommandsMenu();
    commandsMenu.setAutoOpen(true);
    return commandsMenu;
  }
}
